package B刷题.二分;

/**
 * @author wei.zhao
 * @description: 162.寻找峰值
 * @date 2022年04月02日 14:50
 */
public class Code04_FindPeakElement {

    /**
     * 题解：等价于找局部最大值
     */
    public static int findPeakElement(int[] nums) {
        if (nums.length == 1 || nums[0] > nums[1]) {
            return 0;
        }
        if (nums[nums.length - 1] > nums[nums.length - 2]) {
            return nums.length - 1;
        }
        int left = 0;
        int right = nums.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (mid == 0) {
                return 1;
            }
            if ((nums[mid] > nums[mid - 1] && nums[mid] > nums[mid + 1])) {
                return mid;
            }
            if (nums[mid] > nums[mid - 1]) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return -1;
    }

}
